#ifndef QSERVER_EVENT_HPP__
#define QSERVER_EVENT_HPP__

namespace qserv
{
    class event
    {
    public:
        typedef enum tagState
        {
            none = 0,
            parent_open,
            parent_read,
            parent_write,
            parent_close,
            child_open,
            child_read,
            child_write,
            child_close,
            user = 1000,
            quote,
        }state_t;

        typedef enum tagFormat
        {
            raw = 0,
            zlib = 1,
            fast = 2,
            userfmt = 100,
        }format_t;

        explicit event(state_t state)
            :m_eventState(state),
            m_dataLen(0),
            m_pData(NULL)
        {
            
        }

        /*event(state_t state, std::size_t len)
            :m_eventState(state)
        {
            BOOST_ASSERT(len != 0);
            m_dataLen = len;
            m_pData = new char[len];
        }*/

        event(state_t state,
            std::size_t len,
            char *buf)
            : m_eventState(state)
        {
            BOOST_ASSERT(len != 0);
            BOOST_ASSERT(buf != NULL);
            m_dataLen = len;
            m_pData = new char[len];
            memcpy(m_pData, buf, len);
        }

        event(const event& evt)
        {
            const std::size_t& rLen = evt.getLen();
            const char * rpData = evt.getBuf();
            if(NULL != m_pData)
                delete[] m_pData;
            if(0 != rLen)
            {
                m_pData = new char[rLen];
                memcpy(m_pData, rpData, rLen);
                m_dataLen = rLen;
            }
            else
            {
                m_pData = NULL;
                m_dataLen = 0;
            }
            m_eventState = evt.getEventState();
        }

        const event& operator=(const event& evt)
        {
            const std::size_t& rLen = evt.getLen();
            const char * rpData = evt.getBuf();
            if(&evt == this)
                return *this;
            if(NULL != m_pData)
                delete[] m_pData;
            if(0 != rLen)
            {
                m_pData = new char[rLen];
                memcpy(m_pData, rpData, rLen);
                m_dataLen = rLen;
            }
            else
            {
                m_pData = NULL;
                m_dataLen = 0;
            }
            m_eventState = evt.getEventState();
            return *this;
        }

        ~event()
        {
            if (NULL != m_pData)
            {
                delete[] m_pData;
                m_pData = NULL;
            }
        }

        char * getBuf() const { return m_pData; }
        
        std::size_t getLen() const { return m_dataLen; }

        qserv::event::state_t getEventState() const { return m_eventState; }

        void setFormat(format_t fmt = zlib)
        {
            m_dataFormat = fmt;
        }

        qserv::event::format_t getFormat() const { return m_dataFormat; }
    private:
        char *          m_pData;
        std::size_t     m_dataLen;
        state_t         m_eventState;
        format_t        m_dataFormat;
    };
}
#endif //QSERVER_EVENT_HPP__
